//createDialog
var taskFormat = "{'states':{'rect1':{'type':'state','text':{'text':'task_name'}, 'attr':{ 'x':50, 'y':50, 'width':100, 'height':50}, 'props':{'name':{'value':'task_name'}}}},'paths':{},'props':{'props':{'title':{'value':'{task_name}'},'defname':{'value':'task_name'}}},'position':{'minX':'50','minY':'50','maxX':'200','maxY':'100'}}";
var _G_flow_count = 0;
var _G_speed = 500;
var _g_w, _g_h, $content, $topPath, $body;

function loadFlowMainData(flowId) {
	$(".loading-div").show();
	$.ajax({
		type : "post",
		url : "getWFPanoramaChartByID.do",
		data : {
			'flowId' : flowId
		},
		dataType : "json",
		success : function(data) {
			$(".loading-div").hide();
			if (data.ret == 1) {
				alert(data.msg);
			} else if (data.returns) {
				var json=data["returns"];
				//json.title="主流程";
				json.name="主流程";
				appendFlowView(json);
			}
		},
		error : function() {
			$(".loading-div").hide();
			alert("加载失败.");
		}
	});
}
function loadFlowSubData(node, flowId) {
	$(".loading-div").show();
	var name = node.getData().props.name.value + "-子流程和任务";
	$.ajax({
		type : "post",
		url : "getWFSonPanoramaChartByID.do",
		data : {
			'flowId' : flowId,
			'stepId' : node.getId()
		},
		dataType : "json",
		success : function(data) {
			$(".loading-div").hide();
			if (data.ret == 1) {
				alert(data.msg);
			} else if (data.returns) {
				var datas = data["returns"];
				datas["name"] = name;
				appendFlowView(datas);
			}
		},
		error : function() {
			$(".loading-div").hide();
			alert("加载失败.");
		}
	});
}

function initElement() {
	$body = $(".flow-view-box");
	$topPath = $(".view-box-path");
	$content = $(".view-box-content");
}
function initLayout() {
	_g_w = $(window).width();
	_g_h = $(window).height();
	$body.css({
		"width" : _g_w,
		"height" : _g_h
	});
	$content.css({
		"height" : _g_h - 64
	});
}

function initFlowBox() {

	$content.bind("showBox", function(e, i, count) {
		_G_flow_count = i;
		if (i >= count) {
			return;
		}
		// 清除后面的
		for (var x = i + 1; x <= count; x++) {
			$("#flow_path_" + x).remove();
			if (x == i + 1) {
				// 后一个，右移动
				$("#flow_box_" + x).css({
					"left" : 0
				}).animate({
					"left" : _g_w
				}, _G_speed, function() {
					$(this).remove();
				});
			} else {
				$("#flow_box_" + x).remove();
			}
		}
		// 显示当前的
		$("#flow_path_" + i).addClass("view-box-path-item-active");
		// 当前，右移动
		$("#flow_box_" + i).css({
			"left" : _g_w * -1
		}).animate({
			"left" : 0
		}, _G_speed);
	});
}
function appendFlowView(data) {
	_G_flow_count++;
	$box = $('<div class="flow-view-g-box">').appendTo($content);
	$(".view-box-path-item").removeClass("view-box-path-item-active");
	var $path = $('<div class="view-box-path-item  view-box-path-item-active"><div class="view-box-path-item-corn"><div class="view-box-path-item-corn-inner"></div></div></div>');
	$('<div class="view-box-path-item-text">').text(data.name).appendTo($path);
	$topPath.prepend($path);
	$path.attr("id", "flow_path_" + _G_flow_count);
	$box.attr("id", "flow_box_" + _G_flow_count);

	(function(i) {
		$path.click(function() {
			$content.trigger("showBox", [ i, _G_flow_count ]);
		});
	})(_G_flow_count);

	for ( var z in data.data) {
		var d = data.data[z];
		if (d.type == "workflow") {
			var $flowBox = $('<div class="flow-item-box">').appendTo($box);
			var json = $.extend(true, {
				contextPath : contextPath,
				basePath : contextPath + "/resources/g/workflow/",
				editable : false
			}, d);
			$flowBox.myflow(json);
		} else if (d.type == "task") {
			var $flowBox = $('<div class="flow-item-box">').appendTo($box);
			var taskData = {};
			taskData.type = d.type ;
			taskData.title = d['title'];
			taskData.name = d['title'];
			taskData.restore = myUtil.replaceAll(taskFormat,'task_name',d['title']);
			if (d.status && d.status == 'start') {
				taskData.activeRects = "{rects : [ {'paths' : [],'name' : 'rect1'}] }";
				taskData.historyRects="{}";
				taskData.abendRects="{}";
			} else if (d.status && d.status == 'end') {
				taskData.activeRects="{}";
				taskData.abendRects="{}";
				taskData.historyRects = "{rects : [ {'paths' : [],'name' : 'rect1'}] }";
			} else if (d.status && d.status == 'cancel') {
				taskData.activeRects="{}";
				taskData.historyRects="{}";
				taskData.abendRects = "{rects : [ {'paths' : [],'name' : 'rect1'}] }";
			}
			taskData.stepDataRects = {
				'rect1' : d
			};
			var json = $.extend(true, {
				contextPath : contextPath,
				basePath : contextPath + "/resources/g/workflow/",
				editable : false
			}, taskData);
			$flowBox.myflow(json);

		}
	}
	if (_G_flow_count > 1) {
		$box.css({
			left : _g_w
		}).animate({
			left : 0
		}, _G_speed);
		$("#flow_box_" + (_G_flow_count - 1)).animate({
			left : _g_w * -1
		});
	}
	return $box;
}
function createTuLiItem(b, parent, c, t) {
	var item = b("<div>").addClass("tuli-panel-item inline-block");
	var icon = b("<div>").addClass("tuli-panel-item-icon inline-block").html(
			"&nbsp;");
	if (typeof c == "string") {
		icon.css("background", c);
	} else {
		icon.css(c);
	}
	var text = b("<div>").addClass("tuli-panel-item-text inline-block").text(t);
	item.append(icon).append(text).appendTo(parent);
}